package com.cx.bank.view;

import com.cx.bank.dao.BankDaoInterface;
import com.cx.bank.factory.UserDaoFactory;
import com.cx.bank.handler.MyInVocationHandler;
import com.cx.bank.manager.IManager;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.pojo.Log;
import com.cx.bank.pojo.User;
import com.cx.bank.util.DBHelper;
import com.cx.bank.util.LogUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author jxau_scf
 * @date 2021/7/19 - 17:29
 */
public class IndexView extends JFrame {
    //查询余额,取 款,存 款,转账,退出系统, 操作明细, 冻结账户
    private JButton inquiry, withdrawals, deposit, transfer, exitSystem, operationDetails, frozen;
    private JLabel bank;

    private static User user = null;
    private ManagerImpl manager = null;
    private IManager proxy = null;


    public IndexView(){
        building();
        addlisteners();
    }

    private void building() {
        setLayout(null);

        bank = new JLabel("bank");

        inquiry = new JButton("查询余额");
        withdrawals = new JButton("取 款");
        deposit = new JButton("存 款");
        transfer = new JButton("转 账");
        exitSystem = new JButton("退出系统");
        operationDetails = new JButton("操作明细");
        frozen = new JButton("冻结账户");

        bank.setBounds(250,20,120,50);
        Font font = new Font("圆体", Font.BOLD, 50);
        Font font1 = new Font("圆体", Font.BOLD, 30);
        bank.setFont(font);

        inquiry.setBounds(250, 150, 120, 30);
        withdrawals.setBounds(250, 200, 120, 30);
        deposit.setBounds(250, 250, 120, 30);
        transfer.setBounds(250, 300, 120, 30);
        operationDetails.setBounds(250,350,120,30);
        frozen.setBounds(250, 400, 120, 30);
        exitSystem.setBounds(250, 450, 120, 30);

        add(bank);
        add(inquiry);
        add(withdrawals);
        add(deposit);
        add(transfer);
        add(operationDetails);
        add(frozen);
        add(exitSystem);

        setBounds(600,600,600,600);
        setLocation((2000-getWidth())/2,(1000-getHeight())/2);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);

        user = BankView.getUser();
        UserDaoFactory userDaoFactory = null;
        try {
            userDaoFactory = UserDaoFactory.getInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IOException ex) {
            ex.printStackTrace();
        }
        BankDaoInterface bankDao = userDaoFactory.getBankDao();
        manager = ManagerImpl.getInstance();
        manager.setBankDao(bankDao);

        MyInVocationHandler handler = new MyInVocationHandler();
        handler.setTarget(manager);
        handler.setUserName(user.getUserName());
        proxy = (IManager)handler.getProxy();

    }

    private void addlisteners() {
        //查询余额
        inquiry.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Double money = null;
                try {
//                    money = manager.inquiry(user.getUserName());
                    money = proxy.inquiry(user.getUserName());
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                JOptionPane.showMessageDialog(inquiry, "剩余"+money+"元", "余额", JOptionPane.WARNING_MESSAGE);
            }
        });
        //取款
        withdrawals.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //打开输入金额页面
                new WithDrawalsMoneyView().setVisible(true);
            }
        });
        //存款
        deposit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //打开输入金额页面
                new DepositMoneyView().setVisible(true);
            }
        });

        //转账
        transfer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //打开输入转账页面
                new TransferMoneyView().setVisible(true);
            }
        });

        //查询操作明细
        operationDetails.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    List<Log> logList = LogUtil.queryLogByUserId(user.getUserId());
                    StringBuffer str = new StringBuffer();
                    for (Log log : logList){
                        str.append(log);
                        str.append("\n");
                    }
                    //弹窗
                    JOptionPane.showMessageDialog(operationDetails, str, "操作日志明细", JOptionPane.WARNING_MESSAGE);

                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });

        //冻结账户
        frozen.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //打开冻结账户页面
                new FrozenAccountView().setVisible(true);
            }
        });

        //退出系统
        exitSystem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    manager.exitSystem();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }


}
